package com.xrit.sunnyday.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderSettingService;
import com.xrit.sunnyday.exception.SunnydayException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    public OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting os : orderSettingList) {
            Date orderDate = os.getOrderDate();
            OrderSetting osInDb = orderSettingDao.findByOrderDate(orderDate);
            if (null!=osInDb) {
                if (osInDb.getReservations()>os.getNumber()){
                    throw new SunnydayException(os.getOrderDate()+"中，已预约的人数比你现在设置的可预约人数要大");
            }
                orderSettingDao.updateNumber(os);
        }else {
                orderSettingDao.add(os);
            }
    }
}

    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String month) {
        String startDate=month+"-01";
        return orderSettingDao.getOrderSettingByMonth(startDate);
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //从前台获取日期
        Date orderDate = orderSetting.getOrderDate();
        //通过日期判断预约设置是否存在？
        OrderSetting osInDb = orderSettingDao.findByOrderDate(orderDate);
        //判断
        if (null !=osInDb){
            if (osInDb.getReservations()>orderSetting.getNumber()){
                throw new SunnydayException("最大预约人数不能小已预约人数！");
            }
            orderSettingDao.updateNumber(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }


    }
}
